461. 汉明距离
为保证权益,题目请参考 461. 汉明距离(From LeetCode).
解决方案1
CPP
C++
#include <iostream>
using namespace std;
class Solution {
public:
int hammingDistance(int x, int y) {
uint32_t t = 1;
int res = 0;
for (int i = 0; i < 32; i++) {
if ((x & t) ^ (y & t)){
res ++;
}
t = t << 1;
}
return res;
}
};
int main() {
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22